package com.sloera.holding;

import java.util.ArrayList;

/**
 * Simple container example (produces compiler warnings)
 *
 * @author Nickerg
 * @Description Exception in thread "main" java.lang.ClassCastException:
 * @create 2020-4-30
 */
public class ApplesAndOrangesWithoutGenerics {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        ArrayList apples = new ArrayList();
        for (int i = 0; i < 3; i++) {
            apples.add(new Apple());
        }
        //Not prevented from adding an Orange to apples:
        apples.add(new Orange());
        for (int i = 0; i < apples.size(); i++) {
            System.out.println(((Apple) apples.get(i)).id());
            // Orange is detected only at run time
        }
    }
}

class Apple {
    private static long counter;
    private final long id = counter++;

    public long id() {
        return id;
    }
}

class Orange {
}
